<template>
  <div class="experience-detail">
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="!experience" class="no-data">经验不存在</div>
    <div v-else>
      <!-- 返回按钮 -->
      <div class="back-btn" @click="goBack">
        ← 返回列表
      </div>
      
      <!-- 经验详情 -->
      <div class="detail-card">
        <h1>{{ experience.company_name }} - {{ experience.position }}</h1>
        <div class="meta">
          <span v-if="experience.is_anonymous">匿名用户</span>
          <span v-else>用户{{ experience.user_id }}</span>
          <span>{{ formatDate(experience.created_at) }}</span>
        </div>
        
        <div class="content-section">
          <h3>面试经验</h3>
          <p class="content-text">{{ experience.experience_content }}</p>
        </div>
        
        <div class="content-section" v-if="experience.interview_questions">
          <h3>面试题目</h3>
          <pre class="questions-text">{{ experience.interview_questions }}</pre>
        </div>
      </div>
      
      <!-- 评论区 -->
      <div class="comment-section">
        <h2>评论区 ({{ comments.length }})</h2>
        
        <!-- 发表评论 -->
        <div class="comment-input-box">
          <textarea 
            v-model="newComment"
            placeholder="发表你的看法..."
            class="comment-input"
            rows="4"
          ></textarea>
          <button class="submit-btn" @click="submitComment">发表评论</button>
        </div>
        
        <!-- 评论列表 -->
        <div class="comment-list">
          <div v-for="comment in comments" :key="comment.comment_id" class="comment-item">
            <div class="comment-header">
              <span class="comment-user">用户{{ comment.user_id }}</span>
              <span class="comment-time">{{ formatDate(comment.created_at) }}</span>
            </div>
            <div class="comment-content">{{ comment.comment_content }}</div>
            <div class="comment-actions">
              <button class="reply-btn" @click="toggleReply(comment.comment_id)">回复</button>
            </div>
            
            <!-- 回复输入框 -->
            <div v-if="activeReplyId === comment.comment_id" class="reply-input-box">
              <textarea 
                v-model="replyContent"
                placeholder="回复评论..."
                class="reply-input"
                rows="3"
              ></textarea>
              <div class="reply-actions">
                <button class="submit-btn" @click="submitReply(comment.comment_id)">发表回复</button>
                <button class="cancel-btn" @click="cancelReply">取消</button>
              </div>
            </div>
            
            <!-- 回复列表 -->
            <div v-if="comment.replies && comment.replies.length > 0" class="replies-list">
              <div v-for="reply in comment.replies" :key="reply.comment_id" class="reply-item">
                <div class="comment-header">
                  <span class="comment-user">用户{{ reply.user_id }}</span>
                  <span class="comment-time">{{ formatDate(reply.created_at) }}</span>
                </div>
                <div class="comment-content">{{ reply.comment_content }}</div>
              </div>
            </div>
          </div>
        </div>
        
        <div v-if="comments.length === 0" class="no-comments">暂无评论，快来发表第一条评论吧！</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { interviewAPI } from '@/api/interview'
import useStore from '@/store'

const router = useRouter()
const route = useRoute()
const store = useStore()

const experience = ref(null)
const comments = ref([])
const loading = ref(false)
const newComment = ref('')
const replyContent = ref('')
const activeReplyId = ref(null)

// 加载经验详情
const loadExperience = async () => {
  loading.value = true
  try {
    const response = await interviewAPI.getExperienceDetail(route.params.id)
    
    if (response.code === 200 && response.data) {
      experience.value = response.data
    } else {
      ElMessage.error(response.message || '加载经验详情失败')
    }
  } catch (e) {
    console.error('加载经验详情失败:', e)
    ElMessage.error('加载经验详情失败')
  } finally {
    loading.value = false
  }
}

// 加载评论列表
const loadComments = async () => {
  try {
    const response = await interviewAPI.getComments(route.params.id)
    
    if (response.code === 200 && Array.isArray(response.data)) {
      comments.value = response.data
    }
  } catch (e) {
    console.error('加载评论失败:', e)
  }
}

// 发表评论
const submitComment = async () => {
  if (!newComment.value.trim()) {
    ElMessage.warning('请输入评论内容')
    return
  }
  
  try {
    const response = await interviewAPI.createComment({
      experience_id: parseInt(route.params.id),
      comment_content: newComment.value,
    })
    
    if (response.code === 200) {
      newComment.value = ''
      ElMessage.success('评论成功')
      await loadComments()
    } else {
      ElMessage.error(response.message || '发表评论失败')
    }
  } catch (e) {
    console.error('发表评论失败:', e)
    ElMessage.error('发表评论失败')
  }
}

// 切换回复输入框
const toggleReply = (commentId) => {
  if (activeReplyId.value === commentId) {
    activeReplyId.value = null
    replyContent.value = ''
  } else {
    activeReplyId.value = commentId
    replyContent.value = ''
  }
}

// 取消回复
const cancelReply = () => {
  activeReplyId.value = null
  replyContent.value = ''
}

// 发表回复
const submitReply = async (parentId) => {
  if (!replyContent.value.trim()) {
    ElMessage.warning('请输入回复内容')
    return
  }
  
  try {
    const response = await interviewAPI.createComment({
      experience_id: parseInt(route.params.id),
      parent_id: parentId,
      comment_content: replyContent.value,
    })
    
    if (response.code === 200) {
      replyContent.value = ''
      activeReplyId.value = null
      ElMessage.success('回复成功')
      await loadComments()
    } else {
      ElMessage.error(response.message || '发表回复失败')
    }
  } catch (e) {
    console.error('发表回复失败:', e)
    ElMessage.error('发表回复失败')
  }
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

// 返回列表
const goBack = () => {
  router.back()
}

onMounted(() => {
  loadExperience()
  loadComments()
})
</script>

<style scoped>
.experience-detail {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}

.back-btn {
  color: #667eea;
  cursor: pointer;
  margin-bottom: 20px;
  display: inline-block;
  font-size: 14px;
}

.back-btn:hover {
  text-decoration: underline;
}

.loading, .no-data {
  text-align: center;
  padding: 60px 20px;
  color: #666;
  font-size: 16px;
}

.detail-card {
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 30px;
  margin-bottom: 30px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.detail-card h1 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 24px;
}

.meta {
  display: flex;
  gap: 20px;
  font-size: 14px;
  color: #999;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.content-section {
  margin-bottom: 25px;
}

.content-section h3 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 18px;
}

.content-text {
  color: #666;
  line-height: 1.8;
  white-space: pre-wrap;
  margin: 0;
}

.questions-text {
  color: #666;
  line-height: 1.8;
  white-space: pre-wrap;
  margin: 0;
  font-family: inherit;
  background: #f8f9fa;
  padding: 15px;
  border-radius: 6px;
}

.comment-section {
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.comment-section h2 {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 20px;
}

.comment-input-box {
  margin-bottom: 30px;
}

.comment-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  font-size: 14px;
  resize: vertical;
  font-family: inherit;
  margin-bottom: 10px;
}

.submit-btn {
  padding: 10px 25px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.submit-btn:hover {
  background: #5568d3;
}

.cancel-btn {
  padding: 10px 25px;
  background: #f0f0f0;
  color: #666;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  margin-left: 10px;
}

.cancel-btn:hover {
  background: #e0e0e0;
}

.comment-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.comment-item {
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 20px;
}

.comment-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.comment-user {
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.comment-time {
  font-size: 13px;
  color: #999;
}

.comment-content {
  color: #666;
  line-height: 1.6;
  margin-bottom: 10px;
  white-space: pre-wrap;
}

.comment-actions {
  display: flex;
  gap: 10px;
}

.reply-btn {
  padding: 5px 15px;
  background: transparent;
  color: #667eea;
  border: 1px solid #667eea;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}

.reply-btn:hover {
  background: #667eea;
  color: white;
}

.reply-input-box {
  margin-top: 15px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 6px;
}

.reply-input {
  width: 100%;
  padding: 10px;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  font-size: 14px;
  resize: vertical;
  font-family: inherit;
  margin-bottom: 10px;
}

.reply-actions {
  display: flex;
  gap: 10px;
}

.replies-list {
  margin-top: 15px;
  padding-left: 30px;
  border-left: 2px solid #f0f0f0;
}

.reply-item {
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
}

.reply-item:last-child {
  border-bottom: none;
}

.no-comments {
  text-align: center;
  padding: 40px 20px;
  color: #999;
  font-size: 14px;
}
</style>

